<?php

namespace app\platform\controller\admin;

use app\BaseController;

use app\common\logic\DataSettingLogic;

use app\common\model\AppModel;

use app\common\model\DataConfigModel;
use app\common\model\SmsTplModel;

use app\common\logic\DataLogic;

class Setting extends BaseController{

    

    public function getData(){

        $key = $this->request->param('key');

        if(empty($key)){

            return $this->error('请选择要操作的配置');

        }

        $DataSettingLogic = new DataSettingLogic();

        $datas = $DataSettingLogic->getData($key);

        return $this->success([

            'datas' => $datas

        ]);

    }

    public function saveData(){

        $key = $this->request->param('key');

        if(empty($key)){

            return $this->error('请选择要操作的配置');

        }

        $datas = $this->request->param('datas','', 'security_json_or_array');

        $DataSettingLogic = new DataSettingLogic();

        $DataSettingLogic->saveData($key, $datas);

        return $this->success('保存成功');

    }   

    

    

    public function getSmsTplList()

    {



        $where=[

            ['is_delete','=',0],

        ];

        $sms_tpl_model= new SmsTplModel();

        $order=['add_time'=>'desc'];

        $append=['add_time_format'];

        $data=DataLogic::getDataList($sms_tpl_model,$where,$order,'*',$this->request->limit,$this->request->page,$append);

        return $this->success($data);

    }



    public function showSmsTpl()

    {

        $tpl_id=$this->request->param('tpl_id/d',0);

        if($tpl_id==0) return $this->success($return);

        if(null === $sms_tpl = SmsTplModel::find($tpl_id)) return $this->error('模板不存在');

        if($sms_tpl->is_delete == 1 ) return $this->error('模板不存在');

        $detail=[

            'sms_id'=>$sms_tpl->sms_id,

            'tpl'=>$sms_tpl->tpl,

            'key'=>$sms_tpl->key,

            'effect'=>$sms_tpl->effect,

        ];

        $return['detail']=$detail;

        return $this->success($return);

    }

    public function delSmsTpl(){

         $tpl_id=$this->request->param('tpl_id/d',0);

        if(empty($tpl_id)) return $this->error ('模版不存在');

        if(null === $sms_tpl = SmsTplModel::find($tpl_id)) return $this->error('模板不存在');

        $sms_tpl->is_delete = 1;

        $sms_tpl->save();

        return $this->success('删除成功');

    }



    public function saveSmsTpl()

    {

        



        $tpl_id=$this->request->param('tpl_id/d',0);

        $sms_id=$this->request->param('sms_id');

        $tpl=$this->request->param('tpl');

        $key=$this->request->param('key');

        $effect=$this->request->param('effect');

        if(empty($sms_id)) return $this->error('请输入模板ID');

        if(empty($tpl)) return $this->error('请输入模板内容');

        if(empty($key)) return $this->error('请输入发送的英文KEY');

        if(preg_match('/[^A-Za-z]+/', $key))  return $this->error('英文KEY请输入全字母');

        if(empty($effect)) return $this->error('请输入作用');



        if($tpl_id != 0) {

            if(null === $sms_tpl = SmsTplModel::find($tpl_id)) return $this->error('模板不存在');

            if($sms_tpl->is_delete == 1 ) return $this->error('模板不存在');

            //判断同一个app_id下key的唯一性

            if($sms_tpl->key != $key){

                if(null !== SmsTplModel::where([

                        ['key','=',$key],

                        ['is_delete','=',0]

                    ])->find()){

                    return $this->error('英文key在这个应用中已存在');

                }

            }

        }else{

            $sms_tpl = new SmsTplModel();

            if(null !== SmsTplModel::where([

                    ['key','=',$key],

                    ['is_delete','=',0]

                ])->find()){

                return $this->error('英文key在这个应用中已存在');

            }

        }

        $sms_tpl->sms_id = $sms_id;

        $sms_tpl->tpl=$tpl;

        $sms_tpl->key=$key;

        $sms_tpl->effect=$effect;

        $sms_tpl->save();



        return $this->success('保存成功');

    }

    public function configup(){
        $firewall_status=$this->request->param('firewall_status',null);
        $bond_money=$this->request->param('bond_money',null);
        $credit_money=$this->request->param('credit_money',null);
        $bonus1=$this->request->param('bonus1',null);
        $bonus2=$this->request->param('bonus2',null);
        $is_lock=$this->request->param('is_lock',null);
        $lock_ratio=$this->request->param('lock_ratio',null);
        $llock_setting=$this->request->param('lock_setting',null);
        $charges=$this->request->param('charges',null);
        $settle_ratio=$this->request->param('settle_ratio',null);
        $lock_settle_ratio=$this->request->param('lock_settle_ratio',null);
        $user_money_ratio=$this->request->param('user_money_ratio',null);
        $up=[];
        if($firewall_status!==null) $up['firewall_status']=$firewall_status;
        if($bond_money!==null) $up['bond_money']=$bond_money;
        if($credit_money!==null) $up['credit_money']=$credit_money;
        if($bonus1!==null) $up['bonus1']=$bonus1;
        if($bonus2!==null) $up['bonus2']=$bonus2;
        if($is_lock!==null) $up['is_lock']=$is_lock;
        if($lock_ratio!==null) $up['lock_ratio']=$lock_ratio;
        if($charges!==null) $up['charges']=$charges;
        if($settle_ratio!==null) $up['settle_ratio']=$settle_ratio;
        if($lock_settle_ratio!==null) $up['lock_settle_ratio']=$lock_settle_ratio;
        if($user_money_ratio!==null) $up['user_money_ratio']=$user_money_ratio;
        if($llock_setting!==null) $up['lock_setting']=json_encode($llock_setting);
        if(empty($up)) $this->error('暂无更新');
        if (!empty($up)){
            DataConfigModel::up($up,['id'=>1]);
        }
        return $this->success();
    }
    public function configinfo(){
        $dataconfig=new DataConfigModel();
        $info=$dataconfig->where(['id'=>1])->find();
        $info['lock_setting']=json_decode($info['lock_setting'],true);
        return $this->success($info);
    }

}